import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { PlayerMiniInfo } from "../model/PlayerMiniInfo";
import { WindowEvent } from "../../component/window/WindowEvent";
import { IView } from "../../mvc/interfaces/IView";
import { FriendsModule } from "../../../modules/FriendsModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { EFriendType } from "../../../Message/Game/EFriendType";
import { EFriendReplyResult } from "../../../Message/Game/EFriendReplyResult";
import { ReplyType } from "../view/friends/defin/ReplyType";
import { SApplyMsg } from "../../../Message/Game/SApplyMsg";
import { SChatMsg } from "../../../Message/Game/SChatMsg";
import { EChatType } from "../../../Message/Public/EChatType";
import { PublicMessageConfig } from "../resource/PublicMessageConfig";
import { GlobalStyle } from "../../common/global/GlobalStyle";
import { ClockManager } from "../manager/ClockManager";
import { FriendInfo } from "../model/FriendInfo";
import { GameController } from "../mvc/GameController";
import { UserPlayer } from "../scene/player/entity/UserPlayer";
import { CornucopiaMyInfoWindow } from "../view/friends/cornucopia/CornucopiaMyInfoWindow";
import { SPublicMiniPlayer } from "../../../Message/Public/SPublicMiniPlayer";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { SharedObjectName } from "../view/common/defin/SharedObjectName";
import { IFriendModule } from "../../../modules/interfaces/IFriendModule";
import { Cache } from "../cache/Cache";
import { CareerConfig } from "../resource/CareerConfig";
import { AvatarInfo } from "../resource/info/AvatarInfo";
import { AvatarType } from "../resource/info/AvatarType";
import { ImageInfo } from "../../../com/gengine/resource/info/ImageInfo";
import { SystemSetter } from "../view/system/SystemSetter";
import { SMyCornucopiaInfo } from "../../../Message/Public/SMyCornucopiaInfo";
import { FriendBulkDeleteWindow } from "../view/friends/FriendBulkDeleteWindow";
import { PlayerOpRegister } from "../view/common/menu/PlayerOpRegister";
import { PlayerOpMenuConst } from "../view/common/menu/PlayerOpMenuConst";
import { GameManager } from "../manager/GameManager";
import { ModuleType } from "../view/common/ModuleType";
type int = number;
//class FriendsController
    
    export  class FriendsController extends Controller
    {
        constructor()
        {
            
            super();this.Init();
            return;
        }

        /* internal  */privateChatPlayerOnlineInfoGotHandler(arg1: Object): void
        {
            var loc2=null;
            NetDispatcher.removeCmdListener(ServerCommand.SearchPlayerResult, this.privateChatPlayerOnlineInfoGotHandler);
            var loc1=arg1["isFind"];
            if (!loc1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(40353));
                return;
            }
            if (loc1) 
            {
                loc2 = new PlayerMiniInfo(arg1["player"]);
                if (!loc2.online) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(40353));
                }
            }
            return;
        }

        /* internal  */onBulkWindowClose(arg1: WindowEvent): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.FriendListUpdate, this.updateBulkDelFriends);
            NetDispatcher.removeCmdListener(ServerCommand.FriendAdd, this.updateBulkDelFriends);
            NetDispatcher.removeCmdListener(ServerCommand.FriendRemove, this.updateBulkDelFriends);
            return;
        }

        /* internal  */Init(): void
        {
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._friendModule == null) 
            {
                this._friendModule = new FriendsModule();
                this._friendModule.addEventListener(WindowEvent.SHOW, this.showFriendHandler);
                this._friendModule.addEventListener(WindowEvent.CLOSE, this.closeFriendHandler);
            }
            return this._friendModule;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.SearchPlayerResult, this.onSearchPlayer);
            NetDispatcher.addCmdListener(ServerCommand.FriendApply, this.onFriendApply);
            NetDispatcher.addCmdListener(ServerCommand.CornucopiaFriendBlessToMyCornucopia, this.onFriendBlessToMyCornucopia);
            NetDispatcher.addCmdListener(ServerCommand.CornucopiaMyCornucopiaInfo, this.onMyCornucopiaInfo);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.requestDataHandler);
            Dispatcher.addEventListener(EventName.FriendSearchPlayer, this.searchHandler);
            Dispatcher.addEventListener(EventName.FriendRequestAddFriend, this.requestAddFriendHandler);
            Dispatcher.addEventListener(EventName.FriendRequestAddIntimateFriend, this.requestAddintimateFriendHandler);
            Dispatcher.addEventListener(EventName.FriendRequestLowerToFriend, this.requestLowerToFriendHandler);
            Dispatcher.addEventListener(EventName.FriendReplyAdd, this.replyHandler);
            Dispatcher.addEventListener(EventName.FriendDelete, this.deleteFriendHandler);
            Dispatcher.addEventListener(EventName.FriendMoveToBlackList, this.moveToBlackHandler);
            Dispatcher.addEventListener(EventName.FriendPersonalSet, this.personalSetHandler);
            Dispatcher.addEventListener(EventName.FriendButtonRequest, this.buttonRequestHandler);
            Dispatcher.addEventListener(EventName.CornucopiaGetMyCornucopiaMsg, this.getMyCornucopiaMsgHandler);
            Dispatcher.addEventListener(EventName.CornucopiaGetRewards, this.getRewardsHandler);
            Dispatcher.addEventListener(EventName.CornucopiaBlessToFriend, this.blessToFriendHandler);
            Dispatcher.addEventListener(EventName.CornucopiaCallBless, this.callBlessHandler);
            Dispatcher.addEventListener(EventName.CornucopiaBlessAll, this.onBlessAllHandler);
            Dispatcher.addEventListener(EventName.CornucopiaShowView, this.onShowCornucopiaShowViewHandier);
            Dispatcher.addEventListener(EventName.FriendShowBulkDeleteWindow, this.onShowBulkDeleteWindow);
            Dispatcher.addEventListener(EventName.FriendBulkDel, this.onFriendBulkDel);
            Dispatcher.addEventListener(EventName.FriendBulkDelAddToRight, this.onFriendBulkDelAddToRight);
            Dispatcher.addEventListener(EventName.FriendBulkDelRemoveFromRight, this.onFriendBulkDelRemoveFromRight);
            Dispatcher.addEventListener(EventName.FriendPrivateChatRecord, this.privateChatRecordUpdateHandler);
            Dispatcher.addEventListener(EventName.FriendPrivateChatDBClick, this.dbClickRecenetTalkHandler);
            Dispatcher.addEventListener(EventName.FriendPopupFriendModule, this.openFriendModuleHandler);
            return;
        }

        /* internal  */showFriendHandler(arg1: WindowEvent): void
        {
            this.updateFriendList();
            this.updateHeadInfo();
            NetDispatcher.addCmdListener(ServerCommand.EntityInfo_Update, this.updateHeadInfo);
            NetDispatcher.addCmdListener(ServerCommand.EntityInfo_Update, this.judgeLevel);
            NetDispatcher.addCmdListener(ServerCommand.FriendListUpdate, this.updateFriendList);
            NetDispatcher.addCmdListener(ServerCommand.FriendAdd, this.onFriendAdd);
            NetDispatcher.addCmdListener(ServerCommand.FriendRemove, this.onFriendRemove);
            NetDispatcher.addCmdListener(ServerCommand.FriendOnlineStatus, this.onFriendOnlienStatus);
            NetDispatcher.addCmdListener(ServerCommand.FriendUpdate, this.onFriendUpdateStatus);
            NetDispatcher.addCmdListener(ServerCommand.FriendAttributeUpdate, this.onFriendAttributeUpdate);
            Dispatcher.addEventListener(EventName.SexAttributeSexChange, this.updateHeadInfo);
            Dispatcher.addEventListener(EventName.ChangeRoleAvatarHeadIcon, this.updateHeadInfo);
            Dispatcher.addEventListener(EventName.CampChangeSuccess, this.updateHeadInfo);
            return;
        }

        /* internal  */closeFriendHandler(arg1: WindowEvent): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.EntityInfo_Update, this.updateHeadInfo);
            NetDispatcher.removeCmdListener(ServerCommand.EntityInfo_Update, this.judgeLevel);
            NetDispatcher.removeCmdListener(ServerCommand.FriendListUpdate, this.updateFriendList);
            NetDispatcher.removeCmdListener(ServerCommand.FriendAdd, this.onFriendAdd);
            NetDispatcher.removeCmdListener(ServerCommand.FriendRemove, this.onFriendRemove);
            NetDispatcher.removeCmdListener(ServerCommand.FriendOnlineStatus, this.onFriendOnlienStatus);
            NetDispatcher.removeCmdListener(ServerCommand.FriendUpdate, this.onFriendUpdateStatus);
            NetDispatcher.removeCmdListener(ServerCommand.FriendAttributeUpdate, this.onFriendAttributeUpdate);
            Dispatcher.removeEventListener(EventName.SexAttributeSexChange, this.updateHeadInfo);
            Dispatcher.removeEventListener(EventName.CampChangeSuccess, this.updateHeadInfo);
            return;
        }

        /* internal  */searchHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.friendsProxy.SearchPlayer(loc1);
            return;
        }

        /* internal  */requestDataHandler(arg1: Object /* flash.events.Event */): void
        {
            flash.utils.setTimeout(this.requestDataNow, 5000);
            return;
        }

        /* internal  */requestDataNow(): void
        {
            if (!this._isReadFriend) 
            {
                GameProxy.friendsProxy.getAllList();
                this._isReadFriend = true;
            }
            return;
        }

        /* internal  */requestAddFriendHandler(arg1: DataEvent): void
        {
            GameProxy.friendsProxy.applyFriend(arg1.data["name"]);
            return;
        }

        /* internal  */requestAddintimateFriendHandler(arg1: DataEvent): void
        {
            GameProxy.friendsProxy.changefriendType(arg1.data["name"].toString(), EFriendType._EFriendTypeNormal, EFriendType._EFriendtypeIntimate);
            return;
        }

        /* internal  */requestLowerToFriendHandler(arg1: DataEvent): void
        {
            GameProxy.friendsProxy.changefriendType(arg1.data["name"].toString(), EFriendType._EFriendtypeIntimate, EFriendType._EFriendTypeNormal);
            return;
        }

        /* internal  */replyHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["playerId"];
            var loc2=arg1.data["type"];
            var loc3=arg1.data["applyList"];
            var loc4=EFriendReplyResult._EFriendReplyResultAccept;
            var loc5=loc2;
            switch (loc5) 
            {
                case ReplyType.OK:
                {
                    loc4 = EFriendReplyResult._EFriendReplyResultAccept;
                    break;
                }
                case ReplyType.CANCEL:
                {
                    loc4 = EFriendReplyResult._EFriendReplyResultReject;
                    break;
                }
            }
            GameProxy.friendsProxy.reply(loc1, loc4);
            return;
        }

        /* internal  */onBulkWindowShow(arg1: WindowEvent): void
        {
            this.updateBulkWindowFriend();
            NetDispatcher.addCmdListener(ServerCommand.FriendListUpdate, this.updateBulkDelFriends);
            NetDispatcher.addCmdListener(ServerCommand.FriendAdd, this.updateBulkDelFriends);
            NetDispatcher.addCmdListener(ServerCommand.FriendRemove, this.updateBulkDelFriends);
            return;
        }

        /* internal  */addOneKeyFriendMsg(arg1: SApplyMsg): void
        {
            var loc1=0;
            if (this._oneKeyMsgTime < 5) 
            {
                this._oneKeyMsgTime = this._oneKeyMsgTime + 1;
                loc1 = Math.random() * 20000;
                flash.utils.setTimeout(this.OneKeyApplyDelayMsg, 20000 + loc1, arg1);
            }
            return;
        }

        /* internal  */OneKeyApplyDelayMsg(arg1: SApplyMsg): void
        {
            var loc1=new SChatMsg();
            loc1.fromPlayer = arg1.fromPlayer;
            loc1.toPlayerName = arg1.toPlayerName;
            loc1.chatType = EChatType._EChatTypePrivate;
            loc1.content = PublicMessageConfig.instance.getRandomTalkMsg();
            loc1.font = GlobalStyle.color1Uint;
            loc1.chatDt = ClockManager.instance.nowDate;
            loc1.extend = [];
            loc1.playerItems = [];
            loc1.petInfos = [];
            NetDispatcher.dispatchCmd(ServerCommand.ChatMessageUpdate, loc1);
            return;
        }

        /* internal  */deleteFriendHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (typeof arg1.data === "friendinfo") 
            {
                loc1 = arg1.data as FriendInfo;
                GameProxy.friendsProxy.remove(loc1.recordId, loc1.friendFlag);
            }
            return;
        }

        /* internal  */moveToBlackHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as PlayerMiniInfo;
            GameProxy.friendsProxy.addToBlackList(loc1);
            return;
        }

        /* internal  */personalSetHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            cache.personalSetCache.isAutoReply = loc1["isAutoReply"];
            cache.personalSetCache.isNotShowOffline = loc1["isNotShowOffline"];
            cache.personalSetCache.isRefusedToStranger = loc1["isRefusedToStranger"];
            cache.personalSetCache.autoReplyText = loc1["autoReplyText"];
            this.updateAll();
            return;
        }

        /* internal  */buttonRequestHandler(arg1: DataEvent): void
        {
            if (GameController.scene.selectEntity && GameController.scene.selectEntity is UserPlayer) 
            {
                GameProxy.friendsProxy.applyFriend((GameController.scene.selectEntity as UserPlayer).entityInfo.entityInfo.name);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(40388));
            }
            return;
        }

        /* internal  */getMyCornucopiaMsgHandler(arg1: DataEvent): void
        {
            GameProxy.friendsProxy.openMyTreasureBowl();
            return;
        }

        /* internal  */getRewardsHandler(arg1: DataEvent): void
        {
            GameProxy.friendsProxy.getTreasureBowlRewards();
            return;
        }

        /* internal  */blessToFriendHandler(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            GameProxy.friendsProxy.sendTreasureBowlBless(loc1);
            return;
        }

        /* internal  */onBlessAllHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            GameProxy.friendsProxy.sendAllBless(loc1);
            return;
        }

        /* internal  */onShowCornucopiaShowViewHandier(arg1: DataEvent): void
        {
            var loc1=CornucopiaMyInfoWindow.instance;
            if (loc1.isHide) 
            {
                loc1.show();
                Dispatcher.dispatchEvent(new DataEvent(EventName.CornucopiaGetMyCornucopiaMsg));
            }
            else 
            {
                loc1.hide();
            }
            return;
        }

        /* internal  */callBlessHandler(arg1: DataEvent): void
        {
            GameProxy.friendsProxy.treasureBowlCall();
            return;
        }

        /* internal  */updateBulkDelFriends(arg1: Object): void
        {
            this.updateBulkWindowFriend();
            return;
        }

        /* internal  */updateBulkWindowFriend(): void
        {
            this.bulkDeleteWindow.updateFriendList();
            return;
        }

        /* internal  */onFriendBlessToMyCornucopia(arg1: SPublicMiniPlayer): void
        {
            var loc1=new PlayerMiniInfo(arg1);
            if (!PulseSharedObject.isTodayNotTips(SharedObjectName.CornucopiaNotTip)) 
            {
                (view as IFriendModule).showFriendBlessCornucopia(loc1);
            }
            return;
        }

        /* internal  */updateHeadInfo(arg1: Object=null): void
        {
            var obj: Object=null;
            var data: Object;
            var camp: int;
            var sex: int;
            var avatar: int;
            var headImage: Object /* flash.display.Bitmap */;

            var loc1;
            headImage = null;
            obj = arg1;
            data = {};
            data.name = Cache.instance.role.entityInfo.name;
            data.job = CareerConfig.instance.getInfoByCode(Cache.instance.role.entityInfo.careerEnable).name;
            data.level = Cache.instance.role.entityInfo.level;
            camp = Cache.instance.role.entityInfo.camp;
            sex = Cache.instance.role.entityInfo.sex;
            avatar = Cache.instance.role.entityInfo.avatarId;
            if (!(this._lastCamp == camp) || !(this._lastSex == sex) || !(this._lastAvatar == avatar)) 
            {
                headImage = new Object /* flash.display.Bitmap */();
                data.headImage = headImage;
                AvatarInfo.getAvatar(camp, sex, AvatarType.FRIEND, (arg1: ImageInfo): void
                {
                    headImage.bitmapData = arg1.bitmapData;
                    return;
                }, avatar)
            }
            this._lastCamp = camp;
            this._lastSex = sex;
            this._lastAvatar = avatar;
            (view as IFriendModule).updateHeadInfo(data);
            return;
        }

        /* internal  */judgeLevel(arg1: Object): void
        {
            var loc1=Cache.instance.role.entityInfo.level;
            if (loc1 > this._oldLevel) 
            {
                this.updateAll();
            }
            this._oldLevel = loc1;
            return;
        }

        /* internal  */onSearchPlayer(arg1: Object): void
        {
            var loc2=null;
            var loc1=arg1["isFind"];
            if (loc1) 
            {
                loc2 = new PlayerMiniInfo(arg1["player"]);
            }
            (view as IFriendModule).updateSearchResult(loc1, loc2);
            return;
        }

        /* internal  */onFriendApply(arg1: Object): void
        {
            var loc1=null;
            if (typeof arg1 === "sapplymsg") 
            {
                loc1 = new PlayerMiniInfo((arg1 as SApplyMsg).fromPlayer);
                if (SystemSetter.currentSetter.refuseFriend) 
                {
                    GameProxy.friendsProxy.reply(loc1.playerId, EFriendReplyResult._EFriendReplyResultReject);
                }
                else 
                {
                    (view as IFriendModule).showApply(arg1 as SApplyMsg);
                }
            }
            return;
        }

        /* internal  */onMyCornucopiaInfo(arg1: SMyCornucopiaInfo): void
        {
            if (this._init) 
            {
                (view as IFriendModule).showMyCornucopia(arg1);
            }
            return;
        }

        /* internal  */updateFriendList(arg1: Object=null): void
        {
            this.updateAll();
            this._init = true;
            return;
        }

        /* internal  */onFriendAdd(arg1: Object): void
        {
            if (this._init) 
            {
                this.updateAll();
            }
            return;
        }

        /* internal  */onFriendRemove(arg1: Object): void
        {
            if (this._init) 
            {
                this.updateAll();
            }
            return;
        }

        /* internal  */onFriendOnlienStatus(arg1: Object): void
        {
            if (this._init) 
            {
                this.updateAll();
            }
            return;
        }

        /* internal  */onFriendUpdateStatus(arg1: Object): void
        {
            if (this._init) 
            {
                this.updateAll();
            }
            return;
        }

        /* internal  */onFriendAttributeUpdate(arg1: Object): void
        {
            if (this._init) 
            {
                this.updateAll();
            }
            return;
        }

        /* internal  */updateAll(): void
        {
            (view as IFriendModule).updateFriendList(cache.friend.getFriendList(), cache.personalSetCache.isNotShowOffline);
            (view as IFriendModule).updateInitmateFriendList(cache.friend.getIntimateFriendList(), cache.personalSetCache.isNotShowOffline);
            (view as IFriendModule).updateEnemyList(cache.friend.getEnemyList(), cache.personalSetCache.isNotShowOffline);
            (view as IFriendModule).updateBlackList(cache.friend.getBlackList(), cache.personalSetCache.isNotShowOffline);
            (view as IFriendModule).updateRecentList(cache.friend.recentTalkList, cache.personalSetCache.isNotShowOffline);
            return;
        }

        /* internal  */get bulkDeleteWindow(): FriendBulkDeleteWindow
        {
            if (!this._bulkDeleteWindow) 
            {
                this._bulkDeleteWindow = new FriendBulkDeleteWindow();
                this._bulkDeleteWindow.addEventListener(WindowEvent.CLOSE, this.onBulkWindowClose);
                this._bulkDeleteWindow.addEventListener(WindowEvent.SHOW, this.onBulkWindowShow);
            }
            return this._bulkDeleteWindow;
        }

        /* internal  */onShowBulkDeleteWindow(arg1: DataEvent): void
        {
            this.bulkDeleteWindow.show();
            return;
        }

        /* internal  */onFriendBulkDel(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            GameProxy.friendsProxy.bulkDel(loc1);
            return;
        }

        /* internal  */onFriendBulkDelAddToRight(arg1: DataEvent): void
        {
            var loc1=arg1.data as FriendInfo;
            this.bulkDeleteWindow.addFriendToDel(loc1);
            return;
        }

        /* internal  */onFriendBulkDelRemoveFromRight(arg1: DataEvent): void
        {
            var loc1=arg1.data as FriendInfo;
            this.bulkDeleteWindow.cancelFriend(loc1);
            return;
        }

        /* internal  */privateChatRecordUpdateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as PlayerMiniInfo;
            cache.friend.addToRecent(loc1);
            this.updateAll();
            return;
        }

        /* internal  */dbClickRecenetTalkHandler(arg1: DataEvent): void
        {
            PlayerOpRegister.HideOpList();
            var loc1=arg1.data as PlayerMiniInfo;
            var loc2=false;
            var loc3;
            if (loc3 = Cache.instance.friend.getFriendInfoByPlayerName(loc1.playerName)) 
            {
                if (!loc3.friendPlayerInfo.online) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(40353));
                    return;
                }
            }
            NetDispatcher.addCmdListener(ServerCommand.SearchPlayerResult, this.privateChatPlayerOnlineInfoGotHandler);
            PlayerOpMenuConst.Opearte(PlayerOpMenuConst.ChatPirvate, loc1);
            return;
        }

        /* internal  */openFriendModuleHandler(arg1: DataEvent): void
        {
            GameManager.instance.popupWindow(ModuleType.Friend);
            return;
        }

        private /* var */_friendModule: IFriendModule;

        private /* var */_init: Boolean=false;

        private /* var */_oneKeyMsgTime: int=0;

        private /* var */_isReadFriend: Boolean=false;

        private /* var */_lastCamp: int=-1;

        private /* var */_oldLevel: int=-2147483648;

        private /* var */_lastAvatar: int=-1;

        private /* var */_bulkDeleteWindow: FriendBulkDeleteWindow;

        private /* var */_lastSex: int=-1;
    }
